package com.solotiger.douban.ui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.solotiger.douban.MyApplication;
import com.solotiger.douban.common.CommonDialogManager;
import com.solotiger.douban.util.ToastUtil;
import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 所有Fragment的父类
 */
public abstract class BaseFragment extends RxFragment {

    private final static String TAG = "BaseFragment";

    private CommonDialogManager commonDialogManager = null;

    protected View rootView = null;

    protected Unbinder unbinder;

    public CommonDialogManager getCommonDialogManager() {
        if (null == commonDialogManager) {
            commonDialogManager = new CommonDialogManager();
        }
        return commonDialogManager;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutResId(), container, false);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    public void showMessage(int messageResId, Object... args) {
        ToastUtil.show(MyApplication.getInstance(), MyApplication.getInstance().getString(messageResId, args));
    }

    public void showMessage(CharSequence message) {
        ToastUtil.show(MyApplication.getInstance(), message);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        rootView = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    public void loading() {
        getCommonDialogManager().lockLoadData(getFragmentManager());
    }

    public void closeLoading() {
        getCommonDialogManager().unlockLoadData();
    }

    /**
     * 由子类执行后续操作
     *
     * @return
     */
    protected abstract int getLayoutResId();
}
